package com.jinzecloud.reportapproval.controller;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jinzecloud.reportapproval.bean.TestsSummaryTable;
import com.jinzecloud.reportapproval.dto.AuditParam;
import com.jinzecloud.reportapproval.dto.DataStagingParam;
import com.jinzecloud.reportapproval.enums.StateEnum;
import com.jinzecloud.reportapproval.service.AdminService;
import lombok.experimental.Accessors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import com.jinzecloud.reportapproval.bean.VegetationRecordForm;
import com.jinzecloud.reportapproval.service.VegetationRecordFormService;
import com.jinzecloud.reportapproval.utils.PageUtils;
import com.jinzecloud.reportapproval.utils.R;



/**
 * 表11 植被覆盖率测定记录表
 *
 * @author æ­å°
 * @email 1971240051@qq.com
 * @date 2024-12-04 09:52:22
 */
@RestController
@RequestMapping("reportapproval/vegetationrecordform")
@Accessors(chain = true)
public class VegetationRecordFormController {
    @Autowired
    private VegetationRecordFormService vegetationRecordFormService;

    @Autowired
    private AdminService adminService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("reportapproval:vegetationrecordform:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = vegetationRecordFormService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("reportapproval:vegetationrecordform:info")
    public R info(@PathVariable("id") Integer id){
        VegetationRecordForm vegetationRecordForm = vegetationRecordFormService.getById(id);

        return R.ok().put("vegetationRecordForm", vegetationRecordForm);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("reportapproval:vegetationrecordform:save")
    public R save(@RequestBody VegetationRecordForm vegetationRecordForm){

        List<VegetationRecordForm> projectCode = vegetationRecordFormService.list(new QueryWrapper<VegetationRecordForm>().eq("project_code", vegetationRecordForm.getProjectCode()));

        if (projectCode.size() > 0) return R.error("编号："+vegetationRecordForm.getProjectCode()+"已存在，请勿重复添加！");

//        VegetationRecordForm computeVegetationRecordForm = compute(vegetationRecordForm);

        vegetationRecordForm.setStatus(StateEnum.SUBMIT.getState()).setWordPath("").setPdfPath("");
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        vegetationRecordForm.setUserId(adminService.getIdByName(authentication.getName()));

        vegetationRecordFormService.save(vegetationRecordForm);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("reportapproval:vegetationrecordform:update")
    public R update(@RequestBody VegetationRecordForm vegetationRecordForm){
        vegetationRecordFormService.updateById(vegetationRecordForm);

        return R.ok();
    }
    @PostMapping("/auditList")
    public R auditList(@RequestBody DataStagingParam param) {
        return R.ok().setData(vegetationRecordFormService.auditList(param));
    }

    @PostMapping("/review")
    public R review(@RequestBody VegetationRecordForm vegetationRecordForm) {
        vegetationRecordFormService.updateById(new VegetationRecordForm().setId(vegetationRecordForm.getId()).setStatus(0));
        return R.ok();
    }

    @PostMapping("/audit")
    public R audit(@RequestBody AuditParam auditParam) {
        return vegetationRecordFormService.audit(auditParam);
    }


    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("reportapproval:vegetationrecordform:delete")
    public R delete(@RequestBody Integer[] ids){
        vegetationRecordFormService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    @RequestMapping("/viewPdf/{id}")
    public String viewPdf(@PathVariable("id") Integer id){
        return vegetationRecordFormService.viewPdf(id);
    }

    /**
     * 计算
     * @param vegetationRecordForm
     * @return
     */
    public VegetationRecordForm compute(VegetationRecordForm vegetationRecordForm){
        BigDecimal formerArea1 = new BigDecimal(vegetationRecordForm.getFormerArea1());
        BigDecimal formerArea2 = new BigDecimal(vegetationRecordForm.getFormerArea2());
        BigDecimal formerArea3 = new BigDecimal(vegetationRecordForm.getFormerArea3());

        BigDecimal afterArea1 = new BigDecimal(vegetationRecordForm.getAfterArea1());
        BigDecimal afterArea2 = new BigDecimal(vegetationRecordForm.getAfterArea2());
        BigDecimal afterArea3 = new BigDecimal(vegetationRecordForm.getAfterArea3());

        //前平均值
        BigDecimal former =  computeAverage(formerArea1,formerArea2,formerArea3);
        //后平均值
        BigDecimal after =  computeAverage(afterArea1,afterArea2,afterArea3);
        //计算覆盖率
        BigDecimal coverage = computeCoverage(former, after);

        vegetationRecordForm.setFormer(former.toString());
        vegetationRecordForm.setAfter(after.toString());
        vegetationRecordForm.setCoverage(coverage.toString());

        return vegetationRecordForm;
    }

    /**
     * 计算前平均值
     * @param area1
     * @param area2
     * @param area3
     * @return
     */
    private BigDecimal computeAverage(BigDecimal area1, BigDecimal area2, BigDecimal area3) {
        BigDecimal sum = area1.add(area2).add(area3);
        BigDecimal average = sum.divide(new BigDecimal("3"), 1, RoundingMode.HALF_UP);
        return average;
    }

    /**
     * 计算覆盖率
     * @param former
     * @param after
     * @return
     */
    public BigDecimal computeCoverage(BigDecimal former, BigDecimal after){
        BigDecimal coverage = null;

        if (former.compareTo(BigDecimal.ZERO) == 0) {
           coverage = BigDecimal.ZERO;
        }

        // 计算覆盖率 (former - after) / former * 100
        coverage = former.subtract(after).divide(former, 4, RoundingMode.HALF_UP);
        coverage = coverage.multiply(new BigDecimal("100"));
        System.out.println("2=========="+coverage);
        // 四舍五入去掉小数点
        coverage = coverage.setScale(0, RoundingMode.HALF_UP);
        System.out.println("3=========="+coverage);

        return coverage;
    }

}
